package other.xsy.paas;

import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEvent;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEventRequest;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEventResponse;
import com.rkhd.platform.sdk.data.model.Contract;
import com.rkhd.platform.sdk.data.model.Order;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import other.xsy.paas.utils.HCXObjectService;

/**
 * 功能描述：自动流程调用此代码，判断是否有需要删除的订单明细
 *
 * @author whz
 * @ClassName: OrderProductTrigger
 * @Description:
 * @date 2024年03月23日
 */
public class OrderAutoFlowEvent implements AutoFlowEvent {

    private static HCXObjectService hcxObjectService = new HCXObjectService();
    private static Logger logger = LoggerFactory.getLogger();


    @Override
    public AutoFlowEventResponse execute(AutoFlowEventRequest request) throws ScriptBusinessException {
        logger.info("ObjectApiKey:" + request.getEntityApiKey() + " , DataId:" + request.getDataId() + " , ProcessInstanceId:" + request.getProcessInstanceId());
        logger.info(request.toString());
        AutoFlowEventResponse response = new AutoFlowEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        handler(request.getDataId());
        return response;
    }

    private void handler(Long contractId) {
        try {
            Contract contract = new Contract();
            contract.setId(contractId);
            contract = hcxObjectService.get(contract);
            logger.info("合同标题：" + contract.getTitle());
            String sql = "select id,po,contractId,entityType,accountId from _order where contractId = " + contractId + " and customItem210__c = 1";
            Order order = hcxObjectService.queryOne(sql);
            logger.info("订单编号：" + order.getPo() + "  订单信息：" + JSONObject.toJSONString(order));
            // 更新订单编号
            OrderTrigger orderTrigger = new OrderTrigger();
            orderTrigger.handler(order);
        }catch (Exception e) {
            logger.error("系统异常：" + e.getMessage());
        }
    }
}
